package 力扣._1222_可以攻击国王的皇后;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
 
public class Solution {
    public List<List<Integer>> queensAttacktheKing(int[][] queens, int[] king) {
        //从k向8个方向扩散，将遇到的第一个加入列表
        List<List<Integer>> list = new LinkedList<>();
        int[][] qipan = new int[8][8];
        for (int[] queen : queens) {
            qipan[queen[0]][queen[1]] = 1;
        }

        //上
        for (int r = king[0], c = king[1]; 0 <= r && r <= 7 && 0 <= c && c <= 7; r--)
            if (qipan[r][c] == 1) {
                ArrayList<Integer> t = new ArrayList<>(2);
                t.add(r);
                t.add(c);
                list.add(t);
                break;
            }
        //下
        for (int r = king[0], c = king[1]; 0 <= r && r <= 7 && 0 <= c && c <= 7; r++)
            if (qipan[r][c] == 1) {
                ArrayList<Integer> t = new ArrayList<>(2);
                t.add(r);
                t.add(c);
                list.add(t);
                break;
            }
        //左
        for (int r = king[0], c = king[1]; 0 <= r && r <= 7 && 0 <= c && c <= 7; c--)
            if (qipan[r][c] == 1) {
                ArrayList<Integer> t = new ArrayList<>(2);
                t.add(r);
                t.add(c);
                list.add(t);
                break;
            }
        //右
        for (int r = king[0], c = king[1]; 0 <= r && r <= 7 && 0 <= c && c <= 7; c++)
            if (qipan[r][c] == 1) {
                ArrayList<Integer> t = new ArrayList<>(2);
                t.add(r);
                t.add(c);
                list.add(t);
                break;
            }
        //左上
        for (int r = king[0], c = king[1]; 0 <= r && r <= 7 && 0 <= c && c <= 7; r--, c--)
            if (qipan[r][c] == 1) {
                ArrayList<Integer> t = new ArrayList<>(2);
                t.add(r);
                t.add(c);
                list.add(t);
                break;
            }
        //右上
        for (int r = king[0], c = king[1]; 0 <= r && r <= 7 && 0 <= c && c <= 7; r--, c++)
            if (qipan[r][c] == 1) {
                ArrayList<Integer> t = new ArrayList<>(2);
                t.add(r);
                t.add(c);
                list.add(t);
                break;
            }
        //左下
        for (int r = king[0], c = king[1]; 0 <= r && r <= 7 && 0 <= c && c <= 7; r++, c--)
            if (qipan[r][c] == 1) {
                ArrayList<Integer> t = new ArrayList<>(2);
                t.add(r);
                t.add(c);
                list.add(t);
                break;
            }
        //右下
        for (int r = king[0], c = king[1]; 0 <= r && r <= 7 && 0 <= c && c <= 7; r++, c++)
            if (qipan[r][c] == 1) {
                ArrayList<Integer> t = new ArrayList<>(2);
                t.add(r);
                t.add(c);
                list.add(t);
                break;
            }
        return list;
    }
}
